package com.aivin.lib_reader.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.FrameLayout;

import com.aivin.lib_reader.effect.Effect;
import com.aivin.lib_reader.effect.EffectOfRealOneWay;
import com.aivin.lib_reader.tool.ReaderManager;
import com.aivin.lib_reader.icallback.PageChangedCallback;
import com.aivin.lib_reader.ReaderConfig;
import com.aivin.lib_reader.icallback.PageDrawingCallback;


/**
 * 自定义 阅读控件
 */
public class ReaderView extends FrameLayout {
    private ReaderManager mReaderManager;
    private Effect mEffect;
    private ReaderConfig mReaderConfig;
    protected Canvas mCurrPageCanvas;
    protected Canvas mNextPageCanvas;
    /**
     * 当前页
     */
    protected Bitmap mCurrPageBitmap;
    /**
     * 下一页
     */
    protected Bitmap mNextPageBitmap;



    public ReaderView( Context context) {
        this(context, null);
    }

    public ReaderView( Context context,  AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ReaderView( Context context,  AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setWillNotDraw(false); // 开启 viewGroup 的onDraw功能
        mEffect = new EffectOfRealOneWay(context);
        mReaderConfig = new ReaderConfig.Builder().build();
    }



    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);

        int width = getMeasuredWidth();
        int height = getMeasuredHeight();
        if (mCurrPageBitmap == null ||  mNextPageBitmap == null) {
            mCurrPageBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444);
            mNextPageBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444);

            mCurrPageCanvas = new Canvas(mCurrPageBitmap);
            mNextPageCanvas = new Canvas(mNextPageBitmap);
        }

        if (mReaderManager != null) {
            mReaderManager.onAreaChanged(width, height);
            mReaderManager.drawPage(mCurrPageCanvas);
        }
        initEffectConfiguration();
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (mEffect.onTouchEvent(event)) {
            return true;
        }
        return super.onTouchEvent(event);
    }

    @Override
    public void computeScroll() {
        mEffect.computeScroll();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mEffect.onDraw(canvas);
    }


    /**
     * 初始化 Effect 配置
     */
    private void initEffectConfiguration() {
        mEffect.config(getMeasuredWidth(), getMeasuredHeight(), mCurrPageBitmap, mNextPageBitmap);
        mEffect.setPageDrawingCallback(pageDrawingCallback);
    }


    /**
     * 刷新当前 和下一帧
     */
    public void invalidateBothPage() {
        if (mCurrPageCanvas != null && mNextPageCanvas != null) {
            mReaderManager.drawPage(mCurrPageCanvas);
            mReaderManager.drawPage(mNextPageCanvas);
            postInvalidate();
        }
    }

    /**
     * 刷新当前页
     */
    public void invalidateCurrPage() {
        if (mCurrPageCanvas != null) {
            mReaderManager.drawPage(mCurrPageCanvas);
            postInvalidate();
        }
    }


    public ReaderConfig getReaderConfig() {
        return ReaderConfig.newInstance(mReaderConfig);
    }

    public void setReaderManager( ReaderManager readerManager) {
        mReaderManager = readerManager;
        mReaderManager.setReaderView(this, mReaderConfig);
        mReaderManager.notifyDataSetChanged();
    }

    /**
     * 设置正文文字大小
     */
    public void setTextSize(int textSize) {
        if (textSize > 0) {
            ReaderConfig readerConfig = getReaderConfig();
            readerConfig.setTextSize(textSize);
            setReaderConfig(readerConfig);
            invalidateBothPage();
        }
    }


    /**
     * 设置正文行间距
     */
    public void setLineSpace(int lineSpace) {
        if (lineSpace >= 0) {
            ReaderConfig readerConfig = getReaderConfig();
            readerConfig.setLineSpace(lineSpace);
            setReaderConfig(readerConfig);
            invalidateBothPage();
        }
    }

    /**
     * 设置布局配置（包括文字大小、颜色等等所有配置）
     */
    public void setReaderConfig( ReaderConfig readerConfig) {
        this.mReaderConfig = readerConfig;
        mReaderManager.setReaderConfig(mReaderConfig);
    }

    private PageDrawingCallback pageDrawingCallback = new PageDrawingCallback() {
        @Override
        public void invalidate() {
            ReaderView.this.postInvalidate();
        }

        @Override
        public void drawCurrPage() {
            mReaderManager.drawPage( mCurrPageCanvas);
        }

        @Override
        public void drawNextPage() {
            mReaderManager.drawPage( mNextPageCanvas);
        }
    };


    /**
     * 设置翻页回调
     */
    public void setPageChangedCallback( PageChangedCallback pageChangedCallback) {
        mEffect.setPageChangedCallback(pageChangedCallback);
    }

}
